<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/8/9
 * Time: 14:06
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\business;


use app\common\lib\Show;
use app\common\model\mysql\Goods as ModelObj;
use app\common\validate\Goods as ValidateObj;
use think\Exception;
use think\facade\Log;

class Goods extends BusBase
{
    protected ?ModelObj $modelObj = null;
    protected ?ValidateObj $validateObj = null;
    //是否验证允许重复
    protected bool $repeat = true;
    //验证数据库重复字段
    protected string $repeat_string = 'title';

    public function __construct()
    {
        $this->modelObj = new ModelObj();
        $this->validateObj = new ValidateObj();
    }

    /**
     * @param array $data
     * @return bool
     * @throws Exception
     */
    public function add(array $data = []): bool
    {
        //dump($data);exit();

        try {
            $this->valAdd($data);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        $this->modelObj->startTrans();
        try {
            //dump("asdf");exit();
            $data['category_path_id'] = $data['category_id'];
            $array = explode(',', $data['category_id']);
            $data['category_id'] = end($array);
            $data['production_time'] = strtotime($data['production_time']);

            //dump($data);exit();
            if ($data['goods_specs_type'] == 1) {

                $data['skus'][] = [
                    "picture" => $data['big_img'],
                    "price" => $data['price'],
                    "market_price" => $data['market_price'],
                    "cost_price" => $data['cost_price'],
                    "stock" => $data['stock'],
                    "status" => 1,
                ];
                //dump($data);exit();
            } else if ($data['goods_specs_type'] == 2) {
                $stock = array_column($data['skus'], 'stock');
                $data['stock'] = array_sum($stock);
            } else {
                throw new Exception("商品规格错误");
            }
            //dump($data);exit();
            $data['goods_specs_data'] = Show::json_msg($data['skus']);
            $goods_id = $this->modelObj->insertDataByInsertId($data);
            if (!$goods_id) {
                return false;
            }
            $skus = [];

            $goods_sku = (new GoodsSku())->saveAll($data['skus'], (int)$goods_id);

            //dump($goods_sku);exit();
            $carousel = (new GoodsCarousel())->saveAll($data['images'], (int)$goods_id);
            $desc = [
                "description" => $data['description'],
                "goods_id" => $goods_id
            ];
            $description = (new GoodsDescription())->add($desc);
            $this->modelObj->commit();
        } catch (\Exception $e) {
            Log::error("新增商品失败:" . $e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            $this->modelObj->rollback();
            return false;
        }
        //dump($goods_sku);exit();
        return true;
    }
}